// addWine1.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this;
    wx.request({
      url: app.globalData.baseUrl + '/api/house/house_num',
      data: {
        session_key: app.gs()
      },
      success: function (res) {
        console.log(res)
        let houses = res.data.data;
        let houNum;
        if (houses.num.is_scan == 0) {
          let index = 0;
          let hou = [];
          for (let i = 0; i < houses.house.length; i++) {
            hou[i] = houses.house[i].name
          }
          hou.unshift('选择房号');
          _this.setData({
            houses: res.data.data,
            hou: hou,
            num: res.data.data.num,
            houseIndex: '选择房号'
          })
        } else if (houses.num.is_scan == 1) {
          let index = 0;
          let hou = [];
          for (let i = 0; i < houses.house.length; i++) {
            hou[i] = houses.house[i].name
          }
          hou.unshift('选择房号');
          if (houses.num.house == 'undefined') {
            houNum = hou[0]
          } else {
            houNum = houses.num.house
          }
          console.log(houNum)
          _this.setData({
            houses: res.data.data,
            hou: hou,
            num: res.data.data.num,
            houseIndex: res.data.data.num.house,
            houNum: houNum,

          })
        }

      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  callServesice(e){
    wx.request({
      url: app.globalData.baseUrl+'',
    })
  },
  callAddwine(e){
    let _this = this;
    
    let houseIndex = _this.data.houseIndex;
    // console.log(111111)
    // console.log(houseIndex)
    // return
    if (houseIndex=='选择房号'){
      wx.showToast({
        title: '选择房号',
      })
      return
    }else {
      app.wxrequest({
        url: app.globalData.baseUrl + '/api/serve/serve_wine',
        data: {
          session_key: app.gs(),
          house_num: houseIndex
        },
        success(res) {
          wx.hideLoading()
          console.log(res)
          // if (res.data.data == "房间号不能为空！") {
          //   wx.showToast({
          //     title: '房间号不能为空！',
          //   })
          // } else 
         if (res.data.message == 200) {
            wx.showToast({
              title: "呼叫成功！",
            })
          } else if (res.data.data == "呼叫失败！") {
            wx.showToast({
              title: '呼叫失败！',
            })
          }
        }
      })
    }
   
  },
  bindPickerChange: function (e) {
    let _this = this;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let index = e.detail.value;
    console.log(index)
    let ind = parseInt(index) - 1;
    let houseId, house_num;
    let houseIndex = _this.data.houseIndex;
    if (ind == -1) {
       houseId = -1,
       house_num = _this.data.hou[0]
       houseIndex= _this.data.hou[0]
    } else {
      houseId = _this.data.houses.house[ind].id;
      house_num = _this.data.houses.house[ind].name;
      houseIndex = _this.data.houses.house[ind].name
    }
    _this.setData({
      index: index,
      houseId: houseId,
      house_num: house_num,
      houseIndex: houseIndex,
      houNum: house_num
    })
  },
  saoma(e) {
    let _this = this;
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        let scan = res.path;
        let path = scan.split('?')[0];
        //      分配房号-index
        if (path == 'pages/index/index') {
          let house_num = scan.match(/scene=(\S*)/)[1];
        
          wx.request({
            url: app.globalData.baseUrl + '/api/house/band',
           
            data: {
              session_key: app.gs(),
              house_num: house_num
            },
            success(res) {
              console.log('success', res)


              if (res.data.message == 200) {
                wx.showToast({
                  title: '确认完成'
                })
                // setTimeout(function () {
                //   wx.switchTab({
                //     url: '../../index/index'
                //   })
                // }, 2000)
                
                wx.request({
                  url: app.globalData.baseUrl + '/api/house/house_num',
                  data: {
                    session_key: app.gs()
                  },
                  success: function (res) {
                    console.log(res)
                    let houses = res.data.data;
                    let houNum;
                    if (houses.num.is_scan == 0) {
                      let index = 0;
                      let hou = [];
                      for (let i = 0; i < houses.house.length; i++) {
                        hou[i] = houses.house[i].name
                      }
                      hou.unshift('选择房号');
                      _this.setData({
                        houses: res.data.data,
                        hou: hou,
                        num: res.data.data.num,
                        houseIndex: '选择房号'
                      })
                    } else if (houses.num.is_scan == 1) {
                      let index = 0;
                      let hou = [];
                      for (let i = 0; i < houses.house.length; i++) {
                        hou[i] = houses.house[i].name
                      }
                      hou.unshift('选择房号');
                      if (houses.num.house == 'undefined') {
                        houNum = hou[0]
                      } else {
                        houNum = houses.num.house
                      }
                      console.log(houNum)
                      _this.setData({
                        houses: res.data.data,
                        hou: hou,
                        num: res.data.data.num,
                        houseIndex: res.data.data.num.house,
                        houNum: houNum,

                      })
                    }

                  }
                })
              }
            }
          })
        }
      }
    })
  }
})